import axios from "axios";
import main from "./public.js";
import qs from 'qs';
import {
  Indicator,
  Toast
} from "mint-ui";
let loadingCount = 0;
// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    if (loadingCount === 0) {
      // 打开加载框
      Indicator.open({
        text: "加载中...",
        spinnerType: "fading-circle"
      });
    }
    loadingCount++;
    // 在发送请求之前做些什么
    return config;
  },
  function (error) {
    loadingCount++;
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    loadingCount--;
    if (loadingCount == 0) {
      //关闭加载框
      Indicator.close();
    }
    // 对响应数据做点什么
    return response;
  },
  function (error) {
    loadingCount--;
    if (loadingCount == 0) {
      //关闭加载框
      Indicator.close();
    }
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

var obj = {
  code: {
    SUCCESS: "SUCCESS",
    ERROR: "ERROR",
    EXCEPTION: "EXCEPTION",
    backERROR: "1",
    USER_NOT_REG: "USER_NOT_REG",
    ERROR_findOpenid: "ERROR-findOpenid"
  },
  http: {
    post: function (config) {
      if (main.isPlainObject(config) == "Object") {
        var paramsFinal = config["params"];
        var paramsTemp;
        if (paramsFinal == null) {
          paramsTemp = {};
        } else {
          // for (var key in paramsFinal) {
          //   var value = paramsFinal[key];
          //   paramsFinal[key] = encodeURI(value);
          // }
          paramsTemp = (paramsFinal);
        }
        config['type'] = {
          headers: {
            'Content-Type': 'application/json;charset=UTF-8'
          }
        }
        return new Promise((resolve, reject) => {
          axios.post(config["funcName"], JSON.stringify(paramsTemp), config["type"]).then(function (result) {
              var code = result.data["code"];

              if (config['endFunName'] && $.isFunction(config['endFunName'])) {
                // 请求结束执行的函数
                config['endFunName']()
              }

              resolve(result["data"]);

            })
            .catch(err => {
              reject(err);
              console.log(err);
            });
        });
      }
    },
    get: function (config) {
      if (main.isPlainObject(config) == "Object") {
        config["funcName"] = config["funcName"] + ".service";
        axios.get(config["funcName"]).then(function (result) {
          var code = result.data["code"];
          if (code == obj.code.SUCCESS) {
            config.callback([result["data"]]);
          } else if (code.indexOf(obj.code.ERROR) == 0) {
            Toast({
              message: result["data"]["message"],
              position: "bottom",
              duration: 5000
            });
          } else if (code.indexOf(obj.code.EXCEPTION) == 0) {
            Toast({
              message: result["data"]["message"],
              position: "bottom",
              duration: 5000
            });
          }
        });
      }
    }
  }
};

export default obj;